package cn.bgs.service.impl;

import cn.bgs.common.R;
import cn.bgs.mapper.CommunityMapper;
import cn.bgs.pojo.Community;
import cn.bgs.service.ICommunityService;
import cn.bgs.vo.CommunitysVo;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 小区表 服务实现类
 * </p>
 *
 * @author cf
 * @since 2022-06-07
 */
@Service
public class CommunityServiceImpl extends ServiceImpl<CommunityMapper, Community> implements ICommunityService {

    @Autowired
    private CommunityMapper communityMapper;
    @Override
    public List<Community> appFindAllCommunity() {
        return list();
    }


    @Override
    public R findCommunity() {

        List<Community> communityList = communityMapper.findCommunity();

        return R.ok().data("communityList",communityList);
    }

    @Override
    public R addCommunity(CommunitysVo community) {

        System.out.println(community);

        Integer i = communityMapper.addCommunity(community);

        System.out.println(community);

        if (i!=0){

            Integer f = communityMapper.addFile(community.getCommunityId(),community.getFileUrl());

            if (f!=0){
                return R.ok();
            }else {
                return R.error().data("error","提交失败");
            }
        }else {
            return R.error().data("error","提交失败");
        }
    }

    @Override
    public R updateCommunity(CommunitysVo community) {

        Integer i = communityMapper.updateCommunity(community);

        System.out.println(community);

        if (i!=0){
            Integer l = communityMapper.updateFile(community.getCommunityId(),community.getFileUrl());
            if (l!=0){
                return R.ok();
            }else {
                return R.error().data("error","提交失败");
            }
        }else {
            return R.error().data("error","提交失败");
        }
    }

    @Override
    public R delCommunity(Integer id) {

        Integer i = communityMapper.delCommunity(id);

        if (i!=0){
            Integer l = communityMapper.delFile(id);
            if (l!=0){
                return R.ok();
            }else {
                return R.error().data("error","提交失败");
            }
        }else {
            return R.error().data("error","提交失败");
        }
    }
}
